package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.czd.base.utils.WebUtils;
import com.czd.support.model.DataDictionary;
import com.czd.support.model.Helpcenter;
import com.czd.support.model.Manager;
import com.czd.support.model.TreeModel;
import com.czd.support.service.DataDictionaryService;
import com.czd.support.service.HelpcenterService;
import com.czd.support.util.ResponseUtils;

/**
 * @Description 帮助中心控制类
 * @author caoren
 * @Date 2015/05/31
 * @Version 1.0
 */
@Controller
@RequestMapping("/helpcenter")
public class HelpCenterController extends BaseController{
	@Autowired
	private HelpcenterService helpcenterService;
	@Autowired
	private DataDictionaryService dataDictionaryService;

	@RequestMapping("/list")
	public String list() {
		return "/helpcenter/list";
	}
	
	/**
	 * 加载栏目树
	 * @param response
	 */
	@RequestMapping(value = "/treeAll")
	@ResponseBody
	public void treeAll(HttpServletResponse response) {
		List<TreeModel> helpcenterTree = dataDictionaryService.createHelpcenterTree();
		String jsonStr = JSON.toJSONString(helpcenterTree);
		ResponseUtils.renderJson(response,jsonStr);
		
	}
	
	/**
	 * 查询所有的三级子节点
	 * @param level
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getThirdCode/{level}")
	public String getThirdCode(@PathVariable String level, Model model){
		List<DataDictionary> dicDataList = dataDictionaryService.queryListByCode(level);
		model.addAttribute("dicDataList", dicDataList);
		return "/helpcenter/list_child";
	}
	
	/**
	 * 录入内容页面跳转
	 * @param code
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/inputContent/{code}", method = RequestMethod.GET)
	public String inputContent(@PathVariable int code, Model model){
		DataDictionary dataDictionary = dataDictionaryService.findById(code);
		model.addAttribute("dataDictionary", dataDictionary);
		Helpcenter helpcenter = helpcenterService.getByCatetory(code);
		if(helpcenter != null){
			model.addAttribute("helpcenter", helpcenter);
			return "/helpcenter/edit";
		}
		return "/helpcenter/add";
	}
	
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, Helpcenter helpcenter, Model model){
		String level = WebUtils.findParamStr(request, "level");
		Manager manager = this.getSesionUser(request);
		helpcenter.setCreateUser(manager.getUserCode());
		if(helpcenterService.insert(helpcenter)){
			return "redirect:/helpcenter/getThirdCode/"+level;
		}
		return error(model, "内容录入失败", "内容录入失败");
	}
	
	@RequestMapping(value = "/update")
	public String update(HttpServletRequest request, Helpcenter helpcenter, Model model){
		String level = WebUtils.findParamStr(request, "level");
		Manager manager = this.getSesionUser(request);
		helpcenter.setUpdateUser(manager.getUserCode());
		if(helpcenterService.update(helpcenter)){
			return "redirect:/helpcenter/getThirdCode/"+level;
		}
		return error(model, "修改内容失败", "修改内容失败");
	}
}
